package com.xiaobu.go;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import android.os.Handler;

public class StepTimer
{
	private static final long TIMER_DELAY = 1L;
	private static final long TIMER_PERIOD = 1L;
	
	private ScheduledExecutorService executor;
	private Handler handler = new Handler();
	private ArrayList<TimerInterface> interfaceList = new ArrayList<TimerInterface>();
	private ScheduledFuture<?> ticket;
	
	public StepTimer()
	{
		setTimer();
	}
	
	private void countUp()
	{
		synchronized(this)
		{
			try
		    {
		    	Iterator<TimerInterface> iterator = this.interfaceList.iterator();
		    	while (true)
		    	{
		    		if (!iterator.hasNext())
		    		{
		    			break;
		    		}
		    		((TimerInterface)iterator.next()).countUp();
		    	}
		    }
		    finally
		    {
		    }
		}
	}
	
	private void setTimer()
	{
	    this.executor = Executors.newSingleThreadScheduledExecutor();
	    
	    Runnable runnable = new Runnable() {
	    	public void run() {
	    		StepTimer.this.countUp();
	    	}
	    };
	    
	    this.ticket = this.executor.scheduleAtFixedRate(runnable, TIMER_DELAY, TIMER_PERIOD, TimeUnit.SECONDS);
	}
	
	public TimerInterface addMyTimerListener(Runnable listener, boolean periodic, int period)
	{
		TimerInterface timerInterface = null;
		
		synchronized(this)
		{
			try
			{
				timerInterface = new TimerInterface();
				timerInterface.listener = listener;
				timerInterface.periodic = periodic;
				timerInterface.period = period;
				timerInterface.enable = false;
				timerInterface.count = 0;
				
				this.interfaceList.add(timerInterface);
			}
			finally
			{
			}
		}
		
		return timerInterface;
	}
	
	public boolean removeMyTimerListener(TimerInterface timerInterface)
	{
		boolean result = false;
		
		synchronized(this)
		{
			try
			{
				result = this.interfaceList.remove(timerInterface);
			}
			finally
			{
			}
		}
		
		return result;
	}
	
	public void stop()
	{
		this.ticket.cancel(false);
	}
	
	public class TimerInterface
	{
		private int count;
		private boolean enable;
		private Runnable listener;
		private int period;
		private boolean periodic;
		
		public TimerInterface()
		{
		}
		
	    private void countUp()
	    {
	    	synchronized(this)
	    	{
	    		try
		    	{
		    		if (this.enable)
		    		{
		    			this.count = (1 + this.count);
		    			if (this.period < this.count)
		    			{
		    				if (this.listener != null)
		    				{
		    					StepTimer.this.handler.post(this.listener);
		    				}
		    				this.count = 0;
		    				if (!this.periodic)
		    				{
		    					this.enable = false;
		    				}
		    			}
		    		}
		    	}
		    	finally
		    	{
		    	}
	    	}
	    }
	    
	    public void disable()
	    {
	    	synchronized(this)
	    	{
	    		try
		    	{
		    		this.enable = false;
		    		this.count = 0;
		    	}
		    	finally
		    	{
		    	}
	    	}
	    }
	    
	    public void enable()
	    {
	    	synchronized(this)
	    	{
	    		try
		    	{
		    		this.enable = true;
		    	}
		    	finally
		    	{
		    	}
	    	}
	    }
	    
	    public void reset()
	    {
	    	synchronized(this)
	    	{
	    		try
		    	{
		    		this.count = 0;
		    	}
		    	finally
		    	{
		    	}
	    	}
	    }

	    public void reset(int paramInt)
	    {
	    	synchronized(this)
	    	{
	    		try
		    	{
		    		this.period = paramInt;
		    		this.count = 0;
		    	}
		    	finally
		    	{
		    	}
	    	}
	    }
	}
}